package ypmits.videoplayer.view {
	import ypmits.videoplayer.IVideoPicture;
	import ypmits.utils.BitmapAnimator;

	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;

	/**
	 * @author ypmits
	 */
	public class VideoPicture extends Sprite implements IVideoPicture {
		
		private var _width : Number;
		private var _height : Number;
		private var _url : String;
		
		private var bitmap : Bitmap;
		private var bitmapanimator : BitmapAnimator;
		private var _useanimation : Boolean = false;

		
		
		public function VideoPicture( w : Number, h : Number ) {
			_width = w;
			_height = h;
			
			alpha = 0;
		}
		
		
		public function show( delay : Number = 0 ) : void {
			removeEventListener(Event.ENTER_FRAME, enterHideHandler );
			addEventListener(Event.ENTER_FRAME, enterShowHandler );
		}

		
		
		public function hide( delay : Number = 0 ) : void {
			addEventListener(Event.ENTER_FRAME, enterHideHandler );
			removeEventListener(Event.ENTER_FRAME, enterShowHandler );
		}

		
		
		private function enterShowHandler( e : Event ) : void 
		{
			alpha = (alpha + 1 ) * .5;
			if( alpha > .99 ) {
				removeEventListener(Event.ENTER_FRAME, enterShowHandler );
				removeEventListener(Event.ENTER_FRAME, enterHideHandler );
				alpha = 1;
			}
		}

		
		
		private function enterHideHandler( e : Event ) : void 
		{
			alpha = (alpha + 0 ) * .5;
			if( alpha < .01 ) {
				removeEventListener(Event.ENTER_FRAME, enterShowHandler );
				removeEventListener(Event.ENTER_FRAME, enterHideHandler );
				alpha = 0;
			}
		}
		
		
		public function get url() : String {
			return _url;
		}

		
		public function set url( url : String ) : void {
			_url = url;
			
			var l : Loader = new Loader( );
			l.contentLoaderInfo.addEventListener( Event.COMPLETE, pictureurlCompleteHandler );
			l.contentLoaderInfo.addEventListener( ProgressEvent.PROGRESS, pictureurlProgressHandler );
			l.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR, pictureurlErrorHandler );
			l.load( new URLRequest( _url ) );
		}
		
		
		public function setBitmapAnimation( bitmaps : Array, speed : Number = 50, loop : Boolean = false, backandforwards : Boolean = false ) : void {
			_useanimation = true;
			bitmapanimator = new BitmapAnimator( bitmaps, speed, loop, backandforwards);
			addChild( bitmapanimator );
		}

		
		
		public function startBitmapAnimation() : void 
		{
			if( _useanimation ) bitmapanimator.start();
		}



		public function stopBitmapAnimation() : void 
		{
			if( _useanimation ) bitmapanimator.stop();
		}

		
		
		private function pictureurlCompleteHandler( e : Event ) : void {
			bitmap = Bitmap( DisplayObject( LoaderInfo( e.currentTarget ).content ) );
			bitmap.smoothing = true;
			bitmap.width = _width;
			bitmap.height = _height;
			addChild( bitmap );
			
			dispatchEvent( new Event( Event.COMPLETE ) );
		}

		
		private function pictureurlProgressHandler( e : ProgressEvent ) : void {
			dispatchEvent( e );
		}

		
		private function pictureurlErrorHandler( e : IOErrorEvent ) : void {
			dispatchEvent( e );
		}
	}
}
